﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace VehicleScale.Forms.Files
{
    public partial class FormReadLog : TemplateForm
    {

        #region Singleton Members
        private static volatile FormReadLog instance;
        private static object syncRoot = new Object();
        private List<string> _items = new List<string>();
        public static FormReadLog Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new FormReadLog();
                }

                return instance;
            }
        }
        
        #endregion

        #region Contructors
        public FormReadLog()
        {
            InitializeComponent();
            this.Size = new Common().SizeList;
            Initialize();
        }

        public void Initialize()
        {
            
            

            btnExit.Click += new EventHandler(btnExit_Click);
            listDate.SelectedIndexChanged += new EventHandler(listDate_SelectedIndexChanged);
            btnDelete.Click += new EventHandler(btnDelete_Click);
            btnDelAll.Click += new EventHandler(btnDelAll_Click);
            //TestPass();
            //Library.Tools.MsgBox.Show(filePath);
            LoadLog();
        }

       

        public void LoadLog()
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(LogFile.filePath);
                FileInfo[] fileList = dir.GetFiles();
                foreach (var date in fileList)
                {
                    _items.Add(date.ToString());
                }
                listDate.DataSource = _items;
            }
            catch (Exception ex)
            {
                Library.Tools.MsgBox.Show("Lỗi mở Log : " + ex.Message);
            }
        }

        void btnDelAll_Click(object sender, EventArgs e)
        {
            try
            {
                if (Library.Tools.MsgBox.Show("Bạn có chắc muốn xóa tất cả File ", "Cảnh báo", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    LogFile.DeleteAllFile(LogFile.filePath);
                    rtbContent.Clear();
                    _items.RemoveRange(0,_items.Count);

                    listDate.DataSource = null;
                    listDate.DataSource = _items;
                    listDate.Refresh();
                    Library.Tools.MsgBox.Show("Đã xóa File .Chương trình sẽ thoát. Vui lòng mở lại");
                    Application.Exit();
                }
            }
            catch (Exception ex)
            {
                Library.Tools.MsgBox.Show("Lỗi xóa File : " + ex.Message);
            }
            //throw new NotImplementedException();
        }

        void btnDelete_Click(object sender, EventArgs e)
        {
            
            try
            {
                if (listDate.SelectedItem.ToString().Length > 0)
                {
                    string fullname = LogFile.filePath + listDate.SelectedItem.ToString();
                    int index = listDate.SelectedIndex;
                    if (Library.Tools.MsgBox.Show("Bạn có chắc muốn xóa File " + listDate.SelectedItem.ToString(), "Cảnh báo", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        rtbContent.Clear();
                        LogFile.DeleteFile(fullname); 
                        _items.RemoveAt(index);
                        
                        listDate.DataSource = null;
                        listDate.DataSource = _items;
                        listDate.Refresh();
                        
                                              
                    }
                    Library.Tools.MsgBox.Show("Đã xóa File .Chương trình sẽ thoát. Vui lòng mở lại");
                    Application.Exit();
                }
                else
                {
                    Library.Tools.MsgBox.Show("Bạn phải chọn một file để xóa");
                }
            }
            catch (Exception ex)
            {
                Library.Tools.MsgBox.Show("Error : " + ex.Message);
            }
        }

        void listDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            try
            {
                string filename = listDate.SelectedItem.ToString();
                if (filename.Length > 0)
                {
                    string fullname = LogFile.filePath + filename;
                    rtbContent.Clear();
                    rtbContent.Text = LogFile.ReadLog(fullname);
                }
            }
            catch
            {
            }
               
            //Library.Tools.MsgBox.Show(s);
            
        }

        void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }
        #endregion

    }
}
